<?php
	$id_jaminan=$_POST['id_jaminan'];
	$id_peserta=$_POST['id'];
	$tgl_input=date("Y-m-d");
	$tgl_mulai=tgl_ind4($_POST['tgl_mulai']);
	$tgl_selesai=tgl_ind4($_POST['tgl_selesai']);
	$jenis=$_POST['jenis'];
	$catatan=$_POST['catatan'];
	$status=$_POST['status'];
	$id_lama=$_POST['id_lama'];
	$chek=$_POST['kunjungan'];
	
	//RITL
	$no_mr = $_POST['mr'];
	$kelas = $_POST['kelas'];
	$ruang = $_POST['ruang'];
	$diagnosa_awal = $_POST['diagnosa_awal'];
	$diagnosa_keluar = $_POST['diagnosa_keluar'];
	$catatan2 = $_POST['catatan2'];
	
	$ruang2 = $_POST['ruang2'];
	$tgl_masuk = $_POST['tgl_masuk'];
	$tgl_keluar = $_POST['tgl_keluar'];
	
	if(isset($_POST['submit'])){
		if($status == 'Baru' && $chek == '0'){
			$sql="insert into jaminan (id_jaminan,id_peserta,tgl_mulai,tgl_selesai,id_jenis_kunjungan, id_catatan_kunjungan,`status`,id_jaminan_sebelumnya) values ('$id_jaminan','$id_peserta','$tgl_input','$tgl_input',$jenis,$catatan,'$status','')";
		}else if($status == 'Baru' && $chek == ''){
			$sql="insert into jaminan (id_jaminan,id_peserta,tgl_mulai,tgl_selesai,id_jenis_kunjungan, id_catatan_kunjungan,`status`,id_jaminan_sebelumnya) values ('$id_jaminan','$id_peserta','$tgl_mulai','$tgl_selesai',$jenis,$catatan,'$status','')";
		}else if($status == 'Perpanjang' && $chek == '0'){
			$sql="insert into jaminan (id_jaminan,id_peserta,tgl_mulai,tgl_selesai,id_jenis_kunjungan, id_catatan_kunjungan,`status`,id_jaminan_sebelumnya) values ('$id_jaminan','$id_peserta','$tgl_input','$tgl_input',$jenis,$catatan,'$status','$id_lama')";
		}else{
			$sql="insert into jaminan (id_jaminan,id_peserta,tgl_mulai,tgl_selesai,id_jenis_kunjungan, id_catatan_kunjungan,`status`,id_jaminan_sebelumnya) values ('$id_jaminan','$id_peserta','$tgl_mulai','$tgl_selesai',$jenis,$catatan,'$status','$id_lama')";
		}
		mysql_query($sql);
		//echo $sql;
		
		//ID Otomatis
		$query = "SELECT max(id_ritl) as maxID FROM jaminan_ritl";
		$kode = mysql_query($query);
		$data = mysql_fetch_array($kode);
		$idMax = $data['maxID'];
		$idMax++;
		//echo "cc$idMax";
		
		$qry_ritl = "insert into jaminan_ritl() values ($idMax,'$id_jaminan','$no_mr',$kelas,$ruang,'$diagnosa_awal','$diagnosa_keluar','$catatan2')";
		mysql_query($qry_ritl);
		//echo $qry_ritl;
		if(!empty($ruang2)){
			foreach($ruang2 as $key=>$val){
				$sql2="insert into perawatan(id_ritl,id_ruang,tgl_masuk,tgl_keluar) values($idMax,".$val.",'".$tgl_masuk[$key]."','".$tgl_keluar[$key]."')";
				mysql_query($sql2);
				//cho $sql2;
			}
		}
	}
	$url_sukses="module/mod_jaminan/cetak_ritl.php?id=$id_jaminan";
	if($qry){
		echo "<meta http-equiv='refresh' content='0; url=$url_sukses'>";
		exit;
	}else{
		echo mysql_error();
	}
?>